package com.raincat.toolsdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.raincat.tools.multimediapickertools.utils.MultiMediaItem;
import com.raincat.tools.multimediapickertools.utils.PickerConfig;
import com.raincat.tools.multimediapickertools.utils.PickerListener;
import com.raincat.tools.nettools.HttpConfig;
import com.raincat.tools.nettools.HttpsConfig;
import com.raincat.tools.nettools.NetCallBack;
import com.raincat.tools.permissiontools.PermissionConfig;
import com.raincat.tools.permissiontools.PermissionProxyListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2020/03/23
 *     desc   : 说明
 *     version: 1.0
 * </pre>
 */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        byte[] bytes = NeteaseBase64.decode("oemxLYFciUCxHhPcrNS1BoHi/g3fFeh6RI9OGzmujZOFAGtHLXoVgI2SmohuwQKqrYMGnpIyo4w3XkHnqP2C/uyieG1rFJJguJxEOOXh5qKoDjskUpkUjRK2Pi3ZuLsCKWA3sUKUc37WUBvFdSFhFA==");
//        String param = byteToHex(bytes);
//        bytes=hexToByte("04F1A0AB8150EFD085BC839891D19E2E488A2D6C2924C5589260A62E82B1A0D337B4461E3170AEE2994BEECBCEFC210C4BA7D00D569EE24FF6D65FBBDFE1EE116044A9D7F1E1152446C1691676E5E0A67B380631B39DA89D52B320CBCC66AD55A93FE60858526BC005311A4229116787448E4E826128028C366ADEB9837F3C6B75FBAF5E27CD84D331D4EF963ED81B1E6738870D2F09966868402F009E77AE75681B27CBF2061A7A0D136BA7A1199C7E5C01318CE3CDF75C4E338DD84574D22A69EAF96D4268001677767C2F2D5BE100F08188D5DB1D6E3D9FB223A06AA50E61E00BFF085433FA2FDD85E30CCE2857361DFC78719E4D910B7DB74F27B93BBFD10EC19419D19114D89B55C5CAC15C5B88474AAC5CA27CEC9FE789913FB5460BC834988CBBC83313687F0796722526276734105F144FE9D8EE850BBFAC429F8373D539DFE64A22F75555CCB763D4559D35C2B1439EAD7554213C5B5E24CA41223D8F5AA396A0B1CF2D1CAD72E31E4842E61DDA451F2CF223C14208A364B7F0F5A1B6CEE07BE4455DD8491E1A57BACA4D65D1773C16066C2D7FA968DECA29FE920A72A646722B281164132CE582D58F6820CD94937796F6F9FA6B7AFE9268D682CEA4656ECEFE21618566CABFC3FB993B461E99B4921842B9D246F60C32E66764500CCE3569597D5A6E617D4809B47FA60B2DD61065AC9E17885D105699BBE27A99B809698F7642ABBF719D0B64D1779E1B60013F1CF221119F2F8BA038A692DE56A130ABB88508339F22ADB58AFB8141F2A3A47A11FAA2D162786DDC6C860C034933C4745F701671A8FC6B560E6BD46C20");
//        param=Decrypt(bytes,"e82ckenh8dichen8");
//
//        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                try {
//                    HashMap<String, Object> header = new HashMap<>();
//                    header.put("Cookie", "buildver=1584621070; resolution=2029x1080; __remember_me=true; __csrf=ee3c2783893d8f5736d0400276da4d8a; osver=9; deviceId=ODYyNzU2MDQ3Mzc0OTMzCTAyOjAwOjAwOjAwOjAwOjAwCWRjNDc1NTg2NDEwMzM1Y2MJNTI0MDQyYzY%253D; appver=7.1.10; MUSIC_U=39f1966f44c8e22a89fbc463eb1372931280b53c277c1386dd3a549e2fd47d9e9e91dee5e2cb62037c15c83028cd465f8bafcdfe5ad2b092; ntes_kaola_ad=1; versioncode=7001010; mobilename=MI8; os=pc; channel=netease");
//                    header.put("Connection", "keep-alive");
//                    header.put("Content-Type", "application/x-www-form-urlencoded");
//                    header.put("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Mobile Safari/537.36");
//                    header.put("Accept-Encoding", "deflate");
//                    header.put("Accept-Language", "q=0.8,zh-CN;q=0.6,zh;q=0.2");
//                    header.put("Referer", "http://interface3.music.163.com/");
//                    header.put("Host", "interface3.music.163.com");
//                    header.put("Origin", "http://interface3.music.163.com");
//                    header.put("Accept", "*/*");
//                    header.put("Charset", "UTF-8");
//
//
////                    String param = "{\"ids\":\"[" + 22826449 + "]\",\"br\":" + 128000 + ",\"csrf_token\":\"\"}";
//                    String param = "{songid=1433435275}";
////                    param = "params=" + URLEncoder.encode(NeteaseAES.get_params(param), "UTF-8") + "&encSecKey="
////                            + NeteaseAES.get_encSecKey();
//                    param = "params=DE90A23479F6A47565FF93B0ABAF2629B37EA6AAFBA301234F7C0095A86612588EBA536EDDD290E303BFE7D3FE2C82B5DBE038D4CA64E24FA3E15CA6238262D74C54CEF774ADA8B963360FCCF34A6A8685C00986F851388C7308411CB9408EDD56BDABC4A046C46294AB1341623AFE345B179B194DA1F25A11ECE00BB60C2383F76C90CF3D0EDC4D2109B44B33FBD862CB67C7769882895E360A7ABC10D8026D56F8E0FD826ABEC9BEB5B36846EF23F63400B500C639C22CA7C347426BBDE4BA0C5670E4C2730D7EC8511C3409D1104910C1C04406C2107EE52C455B47AF8C160B482D8DA6584B1FB1728B3D41AF97C36CA6B12D4797D3D8515EC77DE3B258A6D5F33A4A0797B6753F0F7D8FB2D391A030F090F71A84A041ADFBCA856242715D6B5CD3A94E0C0D269D00ABBCC4DF8AFF34FA86088A62BE3214DE55ABEA24A8411708A3269192276F6E984E64524268717308C2D1BF5F10956216582EE9AA4E233235D3F6C8E389CC9AAD8FAA43B2972D5C85C44597957B350F1E1AD22000A9821FD6269F8A1B2E340C16A5031087EC9DA52A234F52D6A21B0E01BB76CD4D01FB257C73289520F47023BC60868B89F837F9573594343575B5D84914CAD4F68480284AAE69037E3F4F57196CC2E7D3986853B27D225795094F3DB18026267C7276D6DB4E035971E563EC0E6B55AD7D2987C2211414527899B1DFD2E5F150FC0B3325A31E19588987CF47756E20A2F6C188238F862ECD660555840846E4B5415B90754E37985FB52E0C744A2177749729E00B19EB50CD4431619E243BF317C8541574A6728975DABD6AFA930B23F51544C2";
//
////                    LinkedHashMap<String, Object> param = new LinkedHashMap<>();
////                    String params = "{\"ids\":\"[" + 22826449 + "]\",\"br\":" + 128000 + ",\"csrf_token\":\"\"}";
////                    params = URLEncoder.encode(NeteaseAES.get_params(params), "UTF-8");
////                    param.put("params", params);
////                    param.put("encSecKey", NeteaseAES.get_encSecKey());
//
//                    HttpConfig httpConfig = new HttpConfig();
//                    httpConfig.doPost("http://interface3.music.163.com/eapi/cloud/pub/v2", param)
//                            .headers(header)
//                            .start(MainActivity.this, new NetCallBack() {
//                                @Override
//                                public void finish(JSONObject jsonObject) throws JSONException {
//                                    Log.i("测试", jsonObject.toString());
//                                }
//
//                                @Override
//                                public void error(int errorCode, String error) {
//                                    Log.i("测试", error);
//                                }
//                            });
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
////                byte[] bytes=hexToByte("DE90A23479F6A47565FF93B0ABAF2629B37EA6AAFBA301234F7C0095A86612588EBA536EDDD290E303BFE7D3FE2C82B5DBE038D4CA64E24FA3E15CA6238262D74C54CEF774ADA8B963360FCCF34A6A8685C00986F851388C7308411CB9408EDD56BDABC4A046C46294AB1341623AFE345B179B194DA1F25A11ECE00BB60C2383F76C90CF3D0EDC4D2109B44B33FBD862CB67C7769882895E360A7ABC10D8026D56F8E0FD826ABEC9BEB5B36846EF23F63400B500C639C22CA7C347426BBDE4BA0C5670E4C2730D7EC8511C3409D1104910C1C04406C2107EE52C455B47AF8C160B482D8DA6584B1FB1728B3D41AF97C36CA6B12D4797D3D8515EC77DE3B258A6D5F33A4A0797B6753F0F7D8FB2D391A030F090F71A84A041ADFBCA856242715D6B5CD3A94E0C0D269D00ABBCC4DF8AFF34FA86088A62BE3214DE55ABEA24A8411708A3269192276F6E984E64524268717308C2D1BF5F10956216582EE9AA4E233235D3F6C8E389CC9AAD8FAA43B2972D5C85C44597957B350F1E1AD22000A9821FD6269F8A1B2E340C16A5031087EC9DA52A234F52D6A21B0E01BB76CD4D01FB257C73289520F47023BC60868B89F837F9573594343575B5D84914CAD4F68480284AAE69037E3F4F57196CC2E7D3986853B27D225795094F3DB18026267C7276D6DB4E035971E563EC0E6B55AD7D2987C2211414527899B1DFD2E5F150FC0B3325A31E19588987CF47756E20A2F6C188238F862ECD660555840846E4B5415B90754E37985FB52E0C744A2177749729E00B19EB50CD4431619E243BF317C8541574A6728975DABD6AFA930B23F51544C2");
////                String param=Decrypt(bytes,"e82ckenh8dichen8");
////                MD5 md5=new MD5();
////                String md5String= md5.getMD5ofStr("nobody"+"/api/cloud/pub/v2"+"use"+"{\"songid\":\"1433435275\",\"e_r\":true,\"header\":\"{\\\"os\\\":\\\"pc\\\",\\\"appver\\\":\\\"2.7.1.198242\\\",\\\"deviceId\\\":\\\"ECFB0F5C9DAF813109DC966057D54B616F0D1E20001A7DDA7111\\\",\\\"requestId\\\":\\\"62271990\\\",\\\"clientSign\\\":\\\"60:45:CB:9A:C3:5E@@@WD-WCC2E6LCUS2U@@@@@@39cda0b9-b0aa-4e38-a7d5-e5e9b2f430176d0b275515819c796da324b0129703e2\\\",\\\"osver\\\":\\\"Microsoft-Windows-10-Professional-build-18363-64bit\\\",\\\"MUSIC_U\\\":\\\"39f1966f44c8e22a89fbc463eb1372931280b53c277c1386dd3a549e2fd47d9e9e91dee5e2cb62037c15c83028cd465f8bafcdfe5ad2b092\\\"}\"}"+"md5forencrypt");
////                Log.i("测试", md5String);
//            }
//        });

//        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                HashMap<String, Object> header = new HashMap<>();
//                header.put("Host", "tzdt.qiuguofood.com");
//                header.put("Connection", "keep-alive");
//                header.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; oppo R11 Plus Build/LMY49I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/52.0.2743.100 Mobile Safari/537.36 MMWEBID/5747 MicroMessenger/7.0.15.1680(0x27000F38) Process/appbrand2 WeChat/arm32 NetType/WIFI Language/zh_CN ABI/arm32");
//                header.put("Content-Type", "application/x-www-form-urlencoded");
//                header.put("charset", "utf-8");
//                header.put("Accept-Encoding", "gzip,compress,br,deflate");
//                header.put("token", "F5AD47630DD84C0F7563856EACD9C08DEA046946FBC083207CD7F8C69A47474EEFE11922AAEEE4F3");
//                header.put("Referer", "https://servicewechat.com/wx578e60640fb57da3/4/page-frame.html");
//
//                String param = "tKeyword=3";
//
//                new HttpsConfig().doPost("https://tzdt.qiuguofood.com/topic-data/selectAll", param)
//                        .headers(header)
//                        .start(MainActivity.this, new NetCallBack() {
//                            @Override
//                            public void finish(String jsonObject) {
//                                Log.i("测试", jsonObject);
//                            }
//
//                            @Override
//                            public void error(int errorCode, String error) {
//                                Log.i("测试", error);
//                            }
//                        });
//            }
//        });
    }

    /**
     * hex转byte数组
     *
     * @param hex
     * @return
     */
    public static byte[] hexToByte(String hex) {
        int m = 0, n = 0;
        int byteLen = hex.length() / 2; // 每两个字符描述一个字节
        byte[] ret = new byte[byteLen];
        for (int i = 0; i < byteLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
            ret[i] = Byte.valueOf((byte) intVal);
        }
        return ret;
    }

    /**
     * byte数组转hex
     *
     * @param bytes
     * @return
     */
    public static String byteToHex(byte[] bytes) {
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < bytes.length; n++) {
            strHex = Integer.toHexString(bytes[n] & 0xFF);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示，位数不够，高位补0
        }
        return sb.toString().trim().toUpperCase();
    }

    // 解密
    public static String Decrypt(byte[] encrypted1, String sKey) {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("UTF-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//            byte[] encrypted1 = NeteaseBase64.decode(sSrc);// 先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;
    }
}
